package com.qh.yyxt.service.Impl;

import com.qh.yyxt.pojo.entity.thing.Thing;
import com.qh.yyxt.mapper.ThingMapper;
import com.qh.yyxt.mapper.ThingSizeMapper;
import com.qh.yyxt.pojo.ThingDTO;
import com.qh.yyxt.service.ThingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ThingServiceImpl implements ThingService
{
    @Autowired
    private ThingMapper thingMapper;
    @Autowired
    private ThingSizeMapper thingSizeMapper;
    @Override
    public int addThing(Thing thing)
    {
        if(thing!=null)
        { thingMapper.addThing(thing);
            return thing.getId();
        }
        return 0;
    }
    @Override
    public int updateNum(Integer id,Integer num,Integer renum)
    {
        if(id!=null)
        {
            return thingMapper.updateNum(id,num,renum);
        }
        else{
            return 0;
        }
    }

    @Override
    public int delete(Integer id)
    {
        if(id!=null)
        {
            return thingMapper.delete(id);
        }
        else{
            return 0;
        }
    }


    @Override
    public int updateInfo(Thing thing)
    {
        return thingMapper.updateInfo(thing);
    }

    @Override
    public ThingDTO findById(Integer id)
    {
        Thing thing=thingMapper.findById(id);
        ThingDTO thingDTO=new ThingDTO();
        thingDTO.setDTO(thing,thingSizeMapper.findByTid(id));
        return thingDTO;

    }
    @Override
    public List<ThingDTO> findAllToApply()
    {
        List<Thing>thingList=thingMapper.findAll();
        List<ThingDTO>thingDTOS=new ArrayList<>();
        for (Thing thing:thingList)
        {
            ThingDTO thingDTO=new ThingDTO();
            thingDTO.setDTO(thing,thingSizeMapper.findByTid(thing.getId()));
            thingDTOS.add(thingDTO);
        }
        return thingDTOS;
    }

    @Override
    public List<ThingDTO> findAllToApply(int flag) {
        List<Thing>thingList=thingMapper.findAllByType(flag);
        List<ThingDTO>thingDTOS=new ArrayList<>();
        for (Thing thing:thingList)
        {
            ThingDTO thingDTO=new ThingDTO();
            thingDTO.setDTO(thing,thingSizeMapper.findByTid(thing.getId()));
            thingDTOS.add(thingDTO);
        }
        return thingDTOS;
    }

    @Override
    public Thing findThingById(Integer id) {
        return thingMapper.findById(id);
    }
}
